﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using mySerialPortHelper.model;

namespace mySerialPortHelper
{
    public partial class MainForm : AntdUI.BaseForm
    {
        private readonly SerialConnectModel serialConnectModel;
        private bool serialStatus;
        public MainForm()
        {
            //AutoScaleMode = AutoScaleMode.Dpi;
            InitializeComponent();
            serialConnectModel = new SerialConnectModel();
            InitBindings();
            serialStatus = false;
            serialPort1.DataReceived += SerialPort1DataReceived;
            Debug.WriteLine("串口编码：" + serialPort1.Encoding);
        }

        private void SerialPort1DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            Debug.WriteLine("串口接收到数据");
            string reciveDataStr = "";
            if (reciveDataStr != null)
            {
                if (checkBoxHex.Checked)
                {
                    byte[] reciveDataBytes = new byte[serialPort1.BytesToRead];
                    serialPort1.Read(reciveDataBytes, 0, serialPort1.BytesToRead);
                    reciveDataStr = AppUtil.ConvertByteToHexStr(reciveDataBytes);
                }
                else
                {
                    reciveDataStr = serialPort1.ReadExisting();
                }
                richTextBoxReciveData.AppendText(reciveDataStr + " ");
            }
        }

        private void ButtonOpenPort(object sender, EventArgs e)
        {
            Debug.WriteLine(serialConnectModel.ToString());
            try
            {
                if (!serialStatus)
                {
                    SetSerialPortInstanceArgs(serialPort1, serialConnectModel);
                    serialPort1.Open();
                    if (serialPort1.IsOpen)
                    {
                        serialStatus = true;
                        buttonOpen.Text = "关闭串口";
                        buttonOpen.Image = Properties.Resources.icon_open_32;
                        MessageBox.Show($"串口：{serialConnectModel.PortName} 打开成功");
                    }
                    else
                    {
                        MessageBox.Show($"串口：{serialConnectModel.PortName} 打开失败");
                    }
                }
                else
                {
                    serialPort1.Close();
                    serialStatus = false;
                    buttonOpen.Text = "打开串口";
                    buttonOpen.Image = Properties.Resources.icon_closed_32;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public void SetSerialPortInstanceArgs(SerialPort sp, SerialConnectModel serialConnectModel)
        {
            sp.PortName = serialConnectModel.PortName;
            sp.BaudRate = serialConnectModel.PortBand;
            sp.Parity = serialConnectModel.Parity;
            sp.DataBits = serialConnectModel.DataBits;
            sp.StopBits = serialConnectModel.StopBits;
            sp.RtsEnable = serialConnectModel.Rts;
            sp.DtrEnable = serialConnectModel.Dtr;
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            comboBoxPort.Items.Clear();
            comboBoxPort.Items.AddRange(AppUtil.GetSerialCommsList().ToArray());
        }

        private void InitBindings()
        {
            this.comboBoxPort.DataBindings.Add("Text", serialConnectModel, "PortName");
            this.comboBoxBand.DataBindings.Add("Text", serialConnectModel, "PortBand");
            this.comboBoxPairy.DataBindings.Add("Text", serialConnectModel, "ParityStr");
            this.comboBoxData.DataBindings.Add("Text", serialConnectModel, "DataBits");
            this.comboBoxStop.DataBindings.Add("Text", serialConnectModel, "StopBitsStr");
        }

        private void ManualSendClicked(object sender, EventArgs e)
        {
            string sendData = richTextBoxSendData.Text;
            if (serialPort1.IsOpen)
            {
                serialPort1.Write(sendData);
            }
            else
            {
                MessageBox.Show("请先打开串口！");
            }
        }

        private void ManualClearReciveDataClicked(object sender, EventArgs e)
        {
            richTextBoxReciveData.Clear();
        }

        private void CleatSendDataClicked(object sender, EventArgs e)
        {
            richTextBoxSendData.Clear();
        }

        private void RichTextSendDataChange(object sender, EventArgs e)
        {

        }
    }
}
